﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Basic_dictcollege : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView(true);
        }
    }

    //分页触发事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(true);
    }

    protected void BindGridView(bool GetCount)
    {
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Dict_College", "collegeid", "collegeid,collegename,incharge,regisdate", "collegeid asc", "1=1", GetCount);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        int id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
        string collegename = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text;
        if (e.CommandName == "delcollege")
        {
            int num = dict.Check_DictCollege(collegename);
            if ( num > 0)
            {
                string message = "有" + num.ToString() + "位教师注册了此学院（系），请先删除所有教师！";
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, message);
                return;
            }
            if (dict.Delete_DictCollege(id) > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已删除此学院（系）！");
                BindGridView(true);
            }
        }
    }
    //结束编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView(false);
    }
    //行更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        //获取行主键值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        //获取修改值
        string collegename = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
        if (collegename == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "数据不能为空");
            return;
        }
        dict.Update_DictCollege(collegename, id);
        PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //新增加一个学院
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
         //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        if (this.txtAdd.Text.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "院系名称不能为空！");
            return;
        }
        else
        {
            int result = dict.Insert_DictCollege(this.txtAdd.Text.Trim());
            //写入信息
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "院系添加成功！");
            BindGridView(true);
            this.txtAdd.Text = "";
        }
    }
}
